/*
 * @Author: 杨三炮
 * @Date: 2021-11-18 19:29:50
 * @LastEditors: OBKoro1
 * @LastEditTime: 2021-11-18 20:04:07
 * @FilePath: \node\model\model.js
 */

const mysql = require('mysql')

/**
 * @description: 
 */
const pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'root',
    port: '3306',
    database: 'vue_store'
})

/**
 * 封装一个数据库模型基类
 */
module.exports = class Model {
    /**
     * @description: 通用的查询方法
     * @param {*} sql
     * @param {*} params
     * @return {*}
     */
    static query (sql, params) {
        return new Promise((resolve, reject) => {
            pool.getConnection((err, connection) => {
                if (err) {
                    console.log(err)
                    //结束会话  释放连接
                    connection.release()
                } else {
                    //query执行sql语句
                    connection.query(sql, params, (err, result) => {
                        if (err) {
                            console.log(err)
                            reject(err)
                        } else {
                            resolve(result)
                        }
                        //结束会话  释放连接
                        connection.release()
                    })
                }
            })
        })
    }


    static formatParams () {
        let array = []
        for (let i = 0; i < arguments.length; i++) {
            array.push(arguments[i])
        }
        return array
    }
}